﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="questionEditor.ascx.cs"
    Inherits="Zebra.Umbraco.Marcos.Survey.questionEditor" %>
<div id="dialogHolder">
</div>
<div id="foobar" style="padding: 15px; display: none">
    问题类别<br />
    <select id="subject" name="subject">
        <option value="1" selected="selected">单选</option>
        <option value="2">意见/评论</option>
        <option value="3">不支持啦，别选啦</option>
    </select>
</div>
<div style="padding: 15px; display: none;" id="QuestionEditor">
</div>
<h3>
    编辑调查问卷</h3>
<form class="questionForm" id="Form2" runat="server">
<div class="questionEditor">
    <p>
        <input type="button" class="button" value="增加页面" />
    </p>
    <p class="no-border">
        <input type="button" id="open-custom-dialog" class="button" value="增加问题" />
    </p>
    <div id="QuestionPanel" runat="server" clientidmode="Static">
    </div>
    <p class="no-border">
        <a href="/Survey.aspx">返回我的調查問卷</a> <a href="preview.aspx" target="PreviewWindow"
            onclick="preview(this);">预览</a>
    </p>
</div>
</form>
<script type="text/javascript">
    (function ($) {
        $(function () {
            $("#open-custom-dialog").click(function () {
                $.dialog({
                    title: "新增调查问题",
                    btns: ["cancel", "accept"],
                    defaultBtn: "accept",
                    contentBtnHelp: true,
                    className: "custom-dialog",
                    content: $("#foobar"),
                    father: "#Form2",
                    contentWidth: "500px",
                    contentHeight: "300px",
                    onBeforeAccept: function (event) {
                        OpenQuestionEditor();
                        return true;
                    }
                })
            });
        });
    })(jQuery);
    var editorURL;
    var deleteURL;

    function OpenQuestionEditor() {
        var questionType = $("#subject").val();
        switch (questionType) {
            case "1":
                editorURL = "regularquestioneditor.aspx"
                break;
            case "2":
                editorURL = "commentquestioneditor.aspx";
                break;
            default:
                editorURL = "";
        }
        $.dialog({
            title: "创建问题",
            btns: ["cancel", "accept"],
            defaultBtn: "accept",
            contentBtnHelp: true,
            className: "custom-dialog",
            content: $("#QuestionEditor"),
            father: "#Form2",
            contentWidth: "500px",
            contentHeight: "500px",
            onLoad: function (event) {
                $("#QuestionEditor").load(editorURL);
            },
            onBeforeAccept: function (event) {
                SubmitQuestion(questionType, false, "#QuestionPanel");
            }

        })
    }

    function SubmitQuestion(type, isEdit, targetDIV) {
        switch (type) {
            case "1":
                SubmitRegularQuestion(isEdit, targetDIV);
                break;
            case "2":
                SubmitCommentQuestion(isEdit, targetDIV);
                break;
            default:
        }
    }



    function SubmitRegularQuestion(isEdit, targetDIV) {
        $.post(editorURL, { Question_Title: $("#Question_Title").val(), Question_Choices: $("#Question_Choices").val(), Question_SortingMethod: $("#Question_SortingMethod").val(), Question_MultiSelect: $("#Question_MultiSelect").is(":checked"), Question_MustAnswer: $("#Question_MustAnswer").is(":checked"), Question_AllowComment: $("#Question_AllowComment").is(":checked") },
            function (data) {
                if (isEdit)
                    $(targetDIV).replaceWith(data);
                else
                    $(targetDIV).append(data);
            }
          );
    }

    function SubmitCommentQuestion(isEdit, targetDIV) {
        $.post(editorURL, { Question_Title: $("#Question_Title").val(), Question_MustAnswer: $("#Question_MustAnswer").is(":checked"), Question_RowCount: $("#Question_RowCount").val(), Question_Width: $("#Question_Width").val() },
            function (data) {
                if (isEdit)
                    $(targetDIV).replaceWith(data);
                else
                    $(targetDIV).append(data);
            }
          );
    }

    function EditQuestion(type, questionID, targetDIV) {
        switch (type) {
            case "1":
                //This is regular question editor
                editorURL = "Edit/" + questionID + "/regularquestioneditor.aspx"
                deleteURL = "Delete/" + questionID + "/regularquestioneditor.aspx"
                break;
            case "2":
                editorURL = "Edit/" + questionID + "/commentquestioneditor.aspx"
                deleteURL = "Delete/" + questionID + "/commentquestioneditor.aspx"
                break;
        }
        $.dialog({
            title: "编辑问题",
            btns: ["cancel", "delete", "accept"],
            defaultBtn: "accept",
            contentBtnHelp: true,
            className: "custom-dialog",
            content: $("#QuestionEditor"),
            father: "#Form2",
            contentWidth: "500px",
            contentHeight: "500px",
            onLoad: function (event) {
                $("#QuestionEditor").load(editorURL);
            },
            onBeforeAccept: function (event) {
                SubmitQuestion(type, true, "#" + targetDIV);
            },
            onBeforeDelete: function (event) {
                return confirm("请确认是否删除本题目");
            },
            onDelete: function (event) {
                $.get(deleteURL, function (data) {
                    $("#" + targetDIV).remove();
                }
                );
            }
        })
    }


    var pwin;

    function preview(element) {
        var name = "PreviewWindow";
        if (element) {
            element.target = name;
        }
        if (pwin) {
            pwin.close();
        }
        pwin = window.open('', name, 'resizable=1,scrollbars=1,status=1,menubar=1');
    }
</script>
